/*
 * FechaTypeHandlerCallback
 *
 * Creado el 09/02/2009, 10:46:03 PM
 */
package com.unicon.autoservicio.model;

import com.ibatis.sqlmap.client.extensions.ParameterSetter;
import com.ibatis.sqlmap.client.extensions.ResultGetter;
import com.ibatis.sqlmap.client.extensions.TypeHandlerCallback;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.log4j.Logger;

/**
 * 
 * @author Ricardo Rodríguez
 */
public class FechaTypeHandlerCallback implements TypeHandlerCallback {
   private static Logger logger = Logger.getLogger(FechaTypeHandlerCallback.class.getName());
//   @Override
   public Object getResult(ResultGetter getter)
           throws SQLException {
      Timestamp date = getter.getTimestamp();
      return new Date(date.getTime());
   }

//   @Override
   public void setParameter(ParameterSetter setter, Object parameter)
           throws SQLException {
      Date date = (Date) parameter;
      Timestamp ts = new Timestamp(date.getTime());
      if(logger.isDebugEnabled()){
         logger.debug(ts.toString());
      }
      setter.setTimestamp(ts);
   }

  // @Override
   public Object valueOf(String s) {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      Timestamp date = null;
      try {
         date = new Timestamp(sdf.parse(s).getTime());
      } catch (ParseException ex) {
         date = new Timestamp(System.currentTimeMillis());
      }
      return new Date(date.getTime());
   }
}
